﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.IO;

namespace scc
{
    class Program
    {
        private static SerialPort Connection = new SerialPort();

        static void Main(string[] args)
        {
            // Leemos el puerto que nos indique el usuario.
            String puerto;
            Int32 npuerto = -1;

            System.Console.Write("A que puerto COM te quieres conectar?: ");
            puerto = System.Console.ReadLine();
            try { npuerto = System.Convert.ToInt32(puerto); }
            catch { }

            while (npuerto > 256 || npuerto < 0)
            {
                puerto = System.Console.ReadLine();
                try { npuerto = System.Convert.ToInt32(puerto); }
                catch { }
            }

            // Intentamos conectarnos al puerto
            try
            {
                System.Console.WriteLine("Conectando a puerto Serial...");
                Connection.PortName = "COM" + npuerto;
                Connection.Open();
                Connection.ReadTimeout = 1500;
                System.Console.WriteLine("Conexion correcta en el puerto COM" + npuerto + ".");
            }
            catch { return; }

            // Bucle de lectura - envio - recepción de datos
            while (true)
            {
                System.Console.Write(Environment.NewLine +
                    "_______________________________________________________________" +
                    Environment.NewLine +
                    "Escribe el codigo en Hexadecimal a enviar y pulsa ENTER." +
                    Environment.NewLine +
                    "RE: ");
                String aux = System.Console.ReadLine();

                NXTSendCommandAndGetReply(StringToByteArray(aux));
            }
        }

        private static byte[] StringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }

        private static void NXTSendCommandAndGetReply(byte[] Command)
        {

            Byte[] MessageLength = { 0x00, 0x00 };

            MessageLength[0] = (byte)Command.Length;

            Connection.Write(MessageLength, 0, MessageLength.Length);
            Connection.Write(Command, 0, Command.Length);

            try
            {
                int length = Connection.ReadByte() + 256 * Connection.ReadByte();
                String text;
                text = "RX:";
                for (int i = 0; i < length; i++)
                    text += Connection.ReadByte().ToString("X2") + " ";
                System.Console.WriteLine(text);
            }
            catch { System.Console.WriteLine("Tiempo de espera agotado."); }
        }
    }
}
